//
//  TZYDebrisListViewController.m
//  Moment
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYDebrisListViewController.h"

@interface TZYDebrisListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *fragDetailArray;
@property (nonatomic, assign) BOOL isUpLoad;
@property (nonatomic, assign) NSInteger nextPage;
@property (nonatomic, retain) NSMutableArray *addTimeArray;
@property (nonatomic, assign) CGFloat sizeHeight;

@end

@implementation TZYDebrisListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.nameStr;
    
    
    self.fragDetailArray = [NSMutableArray array];
    self.addTimeArray = [NSMutableArray array];
    
    
    [self createTableView];
    
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
    
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}


- (void)leftButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


//#pragma mark -- 设置下拉刷新
//-(void)addHeaderRefresh
//{
//    //当下拉时候会触发该方法
//    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        NSLog(@"下拉");
//        
//        self.nextPage = 0;// 记录页码的
//        self.isUpLoad = NO;//标记为下拉操作
//        // 重新请求数据
//        [self getAFNetWorkingData];
//        
//    }];
//    
//    // 进到应用自动刷新(可以放在viewdidload)
//    [self.myTableView.mj_header beginRefreshing];
//}


- (void)addHeaderRefresh
{
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderAction)];
    [header setImages:[Singleton shareDataBase].animationArray forState:MJRefreshStateRefreshing];
    
    
    header.lastUpdatedTimeLabel.hidden = YES;
    
    // 隐藏状态
    header.stateLabel.hidden = YES;
    
    
    self.myTableView.mj_header = header;
    
    [self.myTableView.mj_header beginRefreshing];
    
    
}

- (void)refreshHeaderAction
{
    self.nextPage = 0;// 记录页码的
    self.isUpLoad = NO;//标记为下拉操作
    // 重新请求数据
    [self getAFNetWorkingData];
}







#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    //    设置上拉刷新动画
    MJRefreshAutoGifFooter  *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    [footer setImages:[Singleton shareDataBase].animationArray forState:MJRefreshStateRefreshing];
    footer.refreshingTitleHidden = YES;
    footer.stateLabel.hidden = YES;
    
    self.myTableView.mj_footer = footer;
    [self.myTableView.mj_footer beginRefreshing];
}
-(void)refreshAction{
    NSLog(@"上拉");
    self.nextPage += 10;
    self.isUpLoad = YES;
    // 重新请求数据
    [self getAFNetWorkingData];
}





//#pragma mark -- 设置上拉刷新
//-(void)addFooterRefresh
//{
//    //当上拉时候触发该方法
//    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        NSLog(@"上拉");
//        
//        self.nextPage += 10;
//        self.isUpLoad = YES;
//        // 重新请求数据
//        [self getAFNetWorkingData];
//        
//    }];
//    
//}







- (void)getAFNetWorkingData
{
    NSString *urlStr = @"http://api2.pianke.me/timeline/list";
    NSString *para = [NSString stringWithFormat:@"addtime=%@&auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=%ld&tag=%@&version=3.0.6", self.addTimeArray.lastObject, (long)self.nextPage, self.nameStr];
    
    
    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic valueForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            TZYDebrisDetail *debrisDetail = [[TZYDebrisDetail alloc] init];
            [debrisDetail setValuesForKeysWithDictionary:dic];
            [self.fragDetailArray addObject:debrisDetail];
            [self.addTimeArray addObject:[dic objectForKey:@"addtime"]];
            
            
            NSArray *sizeArray = [debrisDetail.coverimg_wh componentsSeparatedByString:@"*"];
            //计算有图片的cell的图片高度
            debrisDetail.bigImageHeight = (SCREEN_WIDTH - 70) * [(sizeArray.lastObject)integerValue] / [(sizeArray.firstObject)integerValue];
           
            //计算markLabel的自适应宽度
            CGSize markLabelWidth = [SelfAdaption labelRectWithSize:CGSizeMake(MAXFLOAT , 20) LabelText:[debrisDetail.tag_info objectForKey:@"tag"] Font:[UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]]];
            
            debrisDetail.markLabelWidth = markLabelWidth.width;
            
            
//            //计算markLabel的自适应宽度
//            CGSize markCountLabelWidth = [SelfAdaption labelRectWithSize:CGSizeMake(MAXFLOAT , 20) LabelText:[debrisDetail.tag_info objectForKey:@"count"] Font:[UIFont systemFontOfSize:[TZYFontSize getFontSizeForSmall]]];
//            
//            debrisDetail.markCountLabelWidth = markCountLabelWidth.width;
        
        }
      
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
}



- (void)createTableView
{
    self.myTableView = [[UITableView alloc] init];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    //self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    

    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[TZYDebrisImageCell class] forCellReuseIdentifier:@"TZYDebrisImageCell"];
    [self.myTableView registerClass:[TZYDebrisListCell class] forCellReuseIdentifier:@"TZYDebrisListCell"];
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TZYDebrisDetail *debrisDetail = [self.fragDetailArray objectAtIndex:indexPath.item];
    
    
    if (![debrisDetail.coverimg_wh isEqualToString:@""]) {
        return ImageCellHeight + debrisDetail.bigImageHeight;
    } else {
        return ListCellHeight;
    }
}


#pragma mark-tableView 的几个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.fragDetailArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    

    TZYDebrisDetail *debrisDetail = [self.fragDetailArray objectAtIndex:indexPath.item];
    
    if (![debrisDetail.coverimg_wh isEqualToString:@""]) {
        
        static NSString *identifier = @"TZYDebrisImageCell";
        TZYDebrisImageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.debrisDetail = debrisDetail;
        
        
        return cell;
    } else {
        
        static NSString *identifier = @"TZYDebrisListCell";
        TZYDebrisListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.debrisDetail = debrisDetail;
        
        return cell;
    }
    
    
    
    return nil;
        

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
